<template>
  <section>
    <search-box>
      <div class="search-box">
        <div class="ip-search">
          <div class="search-cont">
            <!-- <el-input v-model="keyword" placeholder="请输入ip"></el-input>
            <el-button class="m-l-20" type="primary" @click="FnGetIpList">查询</el-button> -->
          </div>
          <div>
            <el-button type="primary" :disabled="FnBtnDisabled('addOutIp')" @click="FnOpenAddIp">新增出网ip</el-button>
          </div>
        </div>
      </div>
    </search-box>
    <el-table :data="ipList" border stripe>
      <el-table-column prop="ip" label="ip列表"></el-table-column>
      <el-table-column prop="describe" label="描述信息"></el-table-column>
      <el-table-column prop="create_time" label="创建时间" width="180"></el-table-column>
      <el-table-column label="操作" width="130">
        <template #default="scope">
          <el-button type="text" @click="FnDeleteIp(scope.row)" :disabled="FnBtnDisabled('deleteIp')">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
              @current-change="handleCurrentChange"
              :current-page="page_index"
              :page-size="page_size"
              layout="total, prev, pager, next"
              :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="新增出网ip"
      v-model="dialogVisible"
      width="500px"
      :before-close="handleClose">
      <el-form :model="formData" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="新增IP:" prop="ip">
          <el-input type="textarea" v-model="formData.ip"></el-input>
        </el-form-item>
        <el-form-item label="描述:" prop="describe">
          <el-input type="textarea" v-model="formData.describe"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="FnAddIp">确 定</el-button>
      </template>
    </el-dialog>
  </section>
</template>

<script>
import searchBox from '../../components/search-box.vue'
import outipService from '@/service/outip'
import {authBtnDisabled} from '@/utils/auth.js'
export default {
  components: { searchBox },
  data() {
    return {
      keyword: '',
      ipList: [],
      page_index: 1,
      page_size: 10,
      total: 0,
      dialogVisible: false,

      formData: {
        ips: [],
        ip: '',
        describe: ''
      },
      rules: {
        ip: [
          {required: true, trigger: ['blur', 'change', 'input'], validator: (rule, value, callback) => {
            if(value === '') {
              callback(new Error('请至少输入一个ip'))
            }
            let reg_ipv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
            let ips = value.split(',')
            let errip = ips.filter(item => {
              return !reg_ipv4.test(item)
            }) 
            if(errip.length > 0) {
              callback(new Error('请输入正确ipv4地址格式，多个地址请用","分割'))
            } else {
              callback()
            }
          }}
        ]
      }
    }
  },
  methods: {
    async FnGetIpList() {
      let params = {
        keyword: this.keyword,
        page_index: this.page_index,
        page_size: 10
      }
      outipService.get_outip_list(params).then(res => {
        if(res.code === 'Success') {
          this.ipList = res.data.ips
          this.total = res.data.total_count
          this.page_index = res.data.page_index
        }
      })
    },
    handleCurrentChange(val) {
      this.page_index = val
      this.FnGetIpList()
    },
    handleClose() {
      this.formData = {
        ips: [],
        ip: '',
        describe: ''
      }
      this.$refs.ruleForm.resetFields()
      this.dialogVisible = false
    },
    FnOpenAddIp() {
      this.dialogVisible = true
    },
    FnAddIp() {
      this.$refs.ruleForm.validate((valid) => {
          if(valid) {
            this.formData.ips = this.formData.ip.split(',')
            let params = {
              ips: this.formData.ips,
              op_type: 'add',
              describe: this.formData.describe
            }
            outipService.gic_out_ip_operate(params).then(res => {
              if(res.code === 'Success') {
                this.$message.success('新增成功！')
                this.handleClose()
                this.FnGetIpList()
              }
            })
          }
      })
    },
    FnDeleteIp(row) {
      this.$alert(`确认删除ip${row.ip}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          callback: () => {
            let params = {
              ips: [row.ip],
              op_type: 'del',
            }
            outipService.gic_out_ip_operate(params).then(res => {
              if(res.code === 'Success') {
                this.$message.success('删除成功！')
                this.FnGetIpList()
              }
            })
          }
        });
    },
    FnBtnDisabled(key) {
      return authBtnDisabled(key, this.$route.meta.name)
    }
  },
  created() {
    this.FnGetIpList()
  }
}
</script>

<style lang="scss" scoped>
.ip-search {
  display: flex;
  justify-content: space-between;
  .search-cont {
    display: flex
  }
}
.page-box {
  margin-top: 10px;
  text-align: right;
}
.dialog-footer {
  text-align: center;
}
</style>